home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rjs.lha / RJS / String / doc / GnuEmacsRegex.txt next >
Text File  |  1991-06-14  |  11KB  |  234 lines

  1.  
  2. Syntax of Regular Expressions
  3. =============================
  4.  
  5. Regular expressions have a syntax in which a few characters are special
  6. constructs and the rest are "ordinary".  An ordinary character is a simple
  7. regular expression which matches that character and nothing else.  The
  8. special characters are `$', `^', `.', `*', `+', `?', `[', `]' and `\'; no
  9. new special characters will be defined.  Any other character appearing in a
  10. regular expression is ordinary, unless a `\' precedes it.
  11.  
  12. For example, `f' is not a special character, so it is ordinary, and
  13. therefore `f' is a regular expression that matches the string `f' and no
  14. other string.  (It does not match the string `ff'.)  Likewise, `o' is a
  15. regular expression that matches only `o'.
  16.  
  17. Any two regular expressions A and B can be concatenated.  The result is a
  18. regular expression which matches a string if A matches some amount of the
  19. beginning of that string and B matches the rest of the string.
  20.  
  21. As a simple example, we can concatenate the regular expressions `f'
  22. and `o' to get the regular expression `fo', which matches only
  23. the string `fo'.  Still trivial.  To do something nontrivial, you
  24. need to use one of the special characters.  Here is a list of them.
  25.  
  26. `. (Period)'     
  27.      is a special character that matches any single character except a
  28.      newline.  Using concatenation, we can make regular expressions like
  29.      `a.b' which matches any three-character string which begins with `a'
  30.      and ends with `b'.
  31.      
  32. `*'     
  33.      is not a construct by itself; it is a suffix, which means the
  34.      preceding regular expression is to be repeated as many times as
  35.      possible.  In `fo*', the `*' applies to the `o', so `fo*' matches one
  36.      `f' followed by any number of `o's.  The case of zero `o's is allowed:
  37.      `fo*' does match `f'.
  38.      
  39.      `*' always applies to the smallest possible preceding expression.
  40.      Thus, `fo*' has a repeating `o', not a repeating `fo'.
  41.      
  42.      The matcher processes a `*' construct by matching, immediately, as
  43.      many repetitions as can be found.  Then it continues with the rest of
  44.      the pattern.  If that fails, backtracking occurs, discarding some of
  45.      the matches of the `*'-modified construct in case that makes it
  46.      possible to match the rest of the pattern.  For example, matching
  47.      `ca*ar' against the string `caaar', the `a*' first tries to match all
  48.      three `a's; but the rest of the pattern is `ar' and there is only `r'
  49.      left to match, so this try fails.  The next alternative is for `a*' to
  50.      match only two `a's.  With this choice, the rest of the regexp matches
  51.      successfully.
  52.      
  53. `+'     
  54.      Is a suffix character similar to `*' except that it requires that the
  55.      preceding expression be matched at least once.  So, for example,
  56.      `ca+r' will match the strings `car' and `caaaar' but not the string
  57.      `cr', whereas `ca*r' would match all three strings.
  58.      
  59. `?'     
  60.      Is a suffix character similar to `*' except that it can match the
  61.      preceding expression either once or not at all.  For example,
  62.      `ca?r' will match `car' or `cr'; nothing else.
  63.      
  64. `[ ... ]'     
  65.      `[' begins a "character set", which is terminated by a `]'.  In the
  66.      simplest case, the characters between the two form the set.  Thus,
  67.      `[ad]' matches either one `a' or one `d', and `[ad]*' matches any
  68.      string composed of just `a's and `d's (including the empty string),
  69.      from which it follows that `c[ad]*r' matches `cr', `car', `cdr',
  70.      `caddaar', etc.
  71.      
  72.      Character ranges can also be included in a character set, by writing
  73.      two characters with a `-' between them.  Thus, `[a-z]' matches any
  74.      lower-case letter.  Ranges may be intermixed freely with individual
  75.      characters, as in `[a-z$%.]', which matches any lower case letter or
  76.      `$', `%' or period.
  77.      
  78.      Note that the usual special characters are not special any more inside
  79.      a character set.  A completely different set of special characters
  80.      exists inside character sets: `]', `-' and `^'.
  81.      
  82.      To include a `]' in a character set, you must make it the first
  83.      character.  For example, `[]a]' matches `]' or `a'.  To include a `-',
  84.      write `---', which is a range containing only `-'.  To include `^',
  85.      make it other than the first character in the set.
  86.      
  87. `[^ ... ]'     
  88.      `[^' begins a "complement character set", which matches any character
  89.      except the ones specified.  Thus, `[^a-z0-9A-Z]' matches all
  90.      characters except letters and digits.
  91.      
  92.      `^' is not special in a character set unless it is the first
  93.      character.  The character following the `^' is treated as if it
  94.      were first (`-' and `]' are not special there).
  95.      
  96.      Note that a complement character set can match a newline, unless
  97.      newline is mentioned as one of the characters not to match.
  98.      
  99. `^'     
  100.      is a special character that matches the empty string, but only if at
  101.      the beginning of a line in the text being matched.  Otherwise it fails
  102.      to match anything.  Thus, `^foo' matches a `foo' which occurs
  103.      at the beginning of a line.
  104.      
  105. `$'     
  106.      is similar to `^' but matches only at the end of a line.  Thus,
  107.      `xx*$' matches a string of one `x' or more at the end of a line.
  108.      
  109. `\'     
  110.      has two functions: it quotes the special characters (including
  111.      `\'), and it introduces additional special constructs.
  112.      
  113.      Because `\' quotes special characters, `\$' is a regular expression
  114.      which matches only `$', and `\[' is a regular expression which matches
  115.      only `[', and so on.
  116.  
  117. Note: for historical compatibility, special characters are treated as
  118. ordinary ones if they are in contexts where their special meanings make no
  119. sense.  For example, `*foo' treats `*' as ordinary since there is no
  120. preceding expression on which the `*' can act.  It is poor practice to
  121. depend on this behavior; better to quote the special character anyway,
  122. regardless of where is appears.
  123.  
  124. For the most part, `\' followed by any character matches only
  125. that character.  However, there are several exceptions: characters
  126. which, when preceded by `\', are special constructs.  Such
  127. characters are always ordinary when encountered on their own.  Here
  128. is a table of `\' constructs.
  129.  
  130. `\|'     
  131.      specifies an alternative.  Two regular expressions A and B with `\|'
  132.      in between form an expression that matches anything that either A or B
  133.      will match.
  134.      
  135.      Thus, `foo\|bar' matches either `foo' or `bar' but no other string.
  136.      
  137.      `\|' applies to the largest possible surrounding expressions.  Only a
  138.      surrounding `\( ... \)' grouping can limit the grouping power of `\|'.
  139.      
  140.      Full backtracking capability exists to handle multiple uses of `\|'.
  141.      
  142. `\( ... \)'     
  143.      is a grouping construct that serves three purposes:
  144.      
  145.        1. To enclose a set of `\|' alternatives for other operations.
  146.           Thus, `\(foo\|bar\)x' matches either `foox' or `barx'.
  147.           
  148.        2. To enclose a complicated expression for the postfix `*' to
  149.           operate on.  Thus, `ba\(na\)*' matches `bananana', etc., with any
  150.           (zero or more) number of `na' strings.
  151.           
  152.        3. To mark a matched substring for future reference.
  153.           
  154.      
  155.      This last application is not a consequence of the idea of a
  156.      parenthetical grouping; it is a separate feature which happens to be
  157.      assigned as a second meaning to the same `\( ... \)' construct
  158.      because there is no conflict in practice between the two meanings.
  159.      Here is an explanation of this feature:
  160.      
  161. `\DIGIT'     
  162.      after the end of a `\( ... \)' construct, the matcher remembers the
  163.      beginning and end of the text matched by that construct.  Then, later
  164.      on in the regular expression, you can use `\' followed by DIGIT to
  165.      mean "match the same text matched the DIGIT'th time by the `\( ... \)'
  166.      construct."
  167.      
  168.      The strings matching the first nine `\( ... \)' constructs appearing
  169.      in a regular expression are assigned numbers 1 through 9 in order that the
  170.      open-parentheses appear in the regular expression.  `\1' through
  171.      `\9' may be used to refer to the text matched by the corresponding
  172.      `\( ... \)' construct.
  173.      
  174.      For example, `\(.*\)\1' matches any newline-free string that is
  175.      composed of two identical halves.  The `\(.*\)' matches the first
  176.      half, which may be anything, but the `\1' that follows must match
  177.      the same exact text.
  178.      
  179. `\`'     
  180.      matches the empty string, provided it is at the beginning
  181.      of the buffer.
  182.      
  183. `\''     
  184.      matches the empty string, provided it is at the end of
  185.      the buffer.
  186.      
  187. `\b'     
  188.      matches the empty string, provided it is at the beginning or end of a
  189.      word.  Thus, `\bfoo\b' matches any occurrence of `foo' as a separate
  190.      word.  `\bballs?\b' matches `ball' or `balls' as a separate word.
  191.      
  192. `\B'     
  193.      matches the empty string, provided it is not at the beginning or
  194.      end of a word.
  195.      
  196. `\<'     
  197.      matches the empty string, provided it is at the beginning of a word.
  198.      
  199. `\>'     
  200.      matches the empty string, provided it is at the end of a word.
  201.      
  202. `\w'     
  203.      matches any word-constituent character.  The editor syntax table
  204.      determines which characters these are.
  205.      
  206. `\W'     
  207.      matches any character that is not a word-constituent.
  208.      
  209. `\sCODE'     
  210.      matches any character whose syntax is CODE.  CODE is a character which
  211.      represents a syntax code: thus, `w' for word constituent, `-' for
  212.      whitespace, `(' for open-parenthesis, etc.  *Note Syntax::.
  213.      
  214. `\SCODE'     
  215.      matches any character whose syntax is not CODE.
  216.  
  217.   Here is a complicated regexp, used by Emacs to recognize the end of a
  218. sentence together with any whitespace that follows.  It is given in Lisp
  219. syntax to enable you to distinguish the spaces from the tab characters.  In
  220. Lisp syntax, the string constant begins and ends with a double-quote.
  221. `\"' stands for a double-quote as part of the regexp, `\\' for a
  222. backslash as part of the regexp, `\t' for a tab and `\n' for a
  223. newline.
  224.  
  225.      "[.?!][]\"')]*\\($\\|\t\\|  \\)[ \t\n]*"
  226.  
  227. This contains four parts in succession: a character set matching period,
  228. `?' or `!'; a character set matching close-brackets,
  229. quotes or parentheses, repeated any number of times; an alternative in
  230. backslash-parentheses that matches end-of-line, a tab or two spaces; and a
  231. character set matching whitespace characters, repeated any number of times.
  232.  
  233.  
  234.